package cn.stronglink.asset.module.sys.permission.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import cn.stronglink.asset.model.Permission;
import cn.stronglink.asset.module.sys.permission.mapper.PermissionMapper;
import cn.stronglink.asset.module.sys.permission.vo.PermissionVo;
import cn.stronglink.asset.module.sys.role.vo.RoleVo;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class PermissionService {
	
	@Autowired
	private PermissionMapper permissionMapper;

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Permission> qryPermission(Map<String, Object> params) {
		return permissionMapper.qryPermission(params);
	}
	
	public void updatePermissionById(Permission permission) {
		permission.setUpdateTime(new Date());
		permission.setUpdateBy(WebUtil.getCurrentUser());
		permissionMapper.updatePermissionById(permission);		
	}

	public void delPermission(Long id) {
		permissionMapper.delPermission(id);		
	}

	public void insertPermission(Permission permission) {
		permission.setId(IdWorker.getId());
		permission.setCreateTime(new Date());
		permission.setCreateBy(WebUtil.getCurrentUser());
		permissionMapper.insertPermission(permission);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Integer getPermissionUsers(Long id) {
		return permissionMapper.getPermissionUsers(id);		
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<PermissionVo> getAllPermission() {
		List<PermissionVo> rootPermission = permissionMapper.getPermissionList(-1l);	
		if(rootPermission!=null&&rootPermission.size()>0){
			for(PermissionVo vo:rootPermission){
				getChildren(vo);
			}
		}
        return rootPermission;  
	}
	
	public void getChildren(PermissionVo vo){
		List<PermissionVo> voList=permissionMapper.getPermissionList(vo.getId());
		if(voList!=null&&voList.size()>0){
			for(PermissionVo child:voList){
				getChildren(child);
			}
			vo.setChildren(voList);
		}
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<String> getRolePermission(Long id) {
        List<String> resultList=new ArrayList<String>();
		List<Long> list=permissionMapper.getRolePermission(id);
		if(list!=null&&list.size()>0){
			for(Long child:list){
				resultList.add(child.toString());
			}
		}
		return resultList;
	}

	public void setRolePermission(RoleVo vo) {
		permissionMapper.delRolePermissionById(vo.getId());
		if(vo.getPermission()!=null&&vo.getPermission().size()>0){
			permissionMapper.createRolePermissionById(vo);
		}
		
	}
	
	
	


	

}
